package com.bw.user_module.edtuser;

import android.text.TextUtils;

import androidx.databinding.ObservableField;
import androidx.room.Room;

import com.bw.base_application_sdk.BaseApplication;
import com.bw.common.entity.SaveEntity;
import com.bw.common.field.EventBusEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.network_sdk.BaseEntity;
import com.bw.user_module.dao.UserDatabase;
import com.bw.user_module.user.UserEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class EdtUserViewModel extends BaseViewModel<EdtUserRespository> {

    public ObservableField<String>nick=new ObservableField<>();
    public ObservableField<Boolean>man=new ObservableField<>();
    public ObservableField<Boolean>woMan=new ObservableField<>();
    public ObservableField<String>phone=new ObservableField<>();
    public ObservableField<String>sign=new ObservableField<>();
    public ObservableField<String>photo = new ObservableField<>();
    private UserEntity userEntity;
    public void selPhoto(){
        EventBusEntity entity = new EventBusEntity();
        entity.action = "photo";
        EventBus.getDefault().post(entity);
    }

    public void save(){
        if (userEntity == null)
            return;
        saveFlag = true;
        //区分用户头像是否需要上传
        if (TextUtils.isEmpty(photo.get())){
            //只更新用户信息
            Map<String,Object>map = new HashMap<>();
            map.put("userName",nick.get()==null?userEntity.data.user_nick_name:nick.get());
            map.put("id",userEntity.data.id);
            map.put("sign",sign.get()==null?userEntity.data.user_sign:sign.get());
            map.put("userIcon",userEntity.data.user_icon);
            if (man.get()){
                userEntity.data.user_gender = "男";
            }else
                userEntity.data.user_gender = "女";
            map.put("gender", userEntity.data.user_gender);
            respository.requestEdtUser(map).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }else {
            //更新头像以及用户信息
            Map<String,Object>map = new HashMap<>();
            map.put("userName",nick.get()==null?userEntity.data.user_nick_name:nick.get());
            map.put("id",userEntity.data.id);
            map.put("sign",sign.get()==null?userEntity.data.user_sign:sign.get());
            map.put("path",photo.get());
            map.put("headName",""+System.currentTimeMillis()+".jpg");
            if (man.get()){
                userEntity.data.user_gender = "男";
            }else
                userEntity.data.user_gender = "女";
            map.put("gender", userEntity.data.user_gender);
            respository.requestUploadImg(map)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }
    }
    private boolean saveFlag = false;
    @Override
    public void onResult(BaseEntity entity) {
        if(entity instanceof UserEntity){
            userEntity = (UserEntity) entity;
            nick.set(userEntity.data.user_nick_name);
            phone.set(userEntity.data.user_name);
            sign.set(userEntity.data.user_sign);
            photo.set(userEntity.data.user_icon);
            if (TextUtils.isEmpty(userEntity.data.user_gender)){
                man.set(true);
                woMan.set(false);
            }else {
                if (userEntity.data.user_gender.equals("男")){
                    man.set(true);
                    woMan.set(false);
                }else {
                    man.set(false);
                    woMan.set(true);
                }
            }
            if (!saveFlag)
                return;
            UserDatabase db = Room.databaseBuilder(BaseApplication.getInstance(),
                    UserDatabase.class, "user.db").build();
            respository.saveUser(userEntity,db)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }else if (entity instanceof SaveEntity){
            if (entity.message.equals("保存成功")){
                EventBusEntity eventBusEntity = new EventBusEntity();
                eventBusEntity.action = "refresh";
                EventBus.getDefault().post(eventBusEntity);
                finishPage();
            }
        }
    }

    @Override
    protected void initData() {
        respository.queryUser().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }
}
